package com.aaa.Oauth2;

import com.aaa.entity.UmsAdmin;
import com.aaa.service.UmsAdminService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UmsAdminService umsAdminService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("s:" + s);
        List<UmsAdmin> list =umsAdminService.selectUser(s);
        System.out.println(list);
        if (list.size()==1) {
            System.out.println(list.get(0));
            UmsAdmin umsAdmin = list.get(0);
            umsAdmin.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(String.valueOf(umsAdmin)));
            return list.get(0);
            } else {
            throw new UsernameNotFoundException("用户名不存在");
        }
    }
}
